<?php

class pam_account
{
    public $authType;

    public function __construct()
    {
        $this->objLibSession = kernel::single('base_session');

        $this->objLibSession->start();

        if( $this->getAttemptRemember() )
        {
            $this->setAttemptRememberExpires();
        }
    }

    public function setAuthType($appId)
    {
        $this->authType = $this->getAuthType($appId);

        return true;
    }

    public function getAuthType($appId = 'desktop' )
    {
        $aType = app::get('pam')->getConf('account_type');
        return $aType[$appId]['type'];
    }

    public function getAttemptRemember()
    {
        if( $_SESSION['account'][$this->authType]['remember'] == 'on' )
        {
            return true;
        }

        return false;
    }

    /**
     * 设置下次自动登录的session有效期
     *
     * @return void
     * @Author 王衍生 50634235@qq.com
     */
    private function setAttemptRememberExpires()
    {
        $minute = 10080;//7天
        $this->objLibSession->set_sess_expires($minute);
        $this->objLibSession->set_cookie_expires($minute);
        return true;
    }

    public function check()
    {
        return $this->getAccountId() ? true : false;
    }

    public function getAccountId()
    {
        return $_SESSION['account'][$this->authType]['id'];
    }
}